﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace Gre_Wp7
{
    public class Engine
    {
        static PageInfo[] _pageInfoArray ;
        static int _totalNumber;
        static int _pageItems;
        static List<PageInfo> _pageInfoList;


        public Engine(int total = 500, int pageItems = 5)
        {
            if (_pageInfoArray == null)
            {
                _totalNumber = total;
                _pageItems = pageItems;

                _pageInfoArray = new PageInfo[_totalNumber / _pageItems];
                for (int i = 0; i < _totalNumber / _pageItems; i++)
                {
                    _pageInfoArray[i] = new PageInfo();
                }
            }
        }


        public Engine(bool flag,int wordNumber = 500, int pageItems =5)
        {
            if (_pageInfoList == null)
            {
                XDocument xdoc = XmlFile.GetXmlDoc();
                int count = xdoc.Root.Elements().Count<XElement>();

                _pageInfoList = new List<PageInfo>();
                _pageItems = 5;
                _totalNumber = count * 5;
                
                

                foreach (XElement xe in xdoc.Root.Elements())
                {
                    int id = Convert.ToInt32(xe.Element("Id").Value);
                    PageInfo pInfo = new PageInfo();

                    pInfo.Id = id;

                    foreach (XElement re in xe.Elements("Rem"))
                    {
                        DateTime d = Convert.ToDateTime(re.Value);
                        pInfo.RemTime.Add(d);
                    }
                    _pageInfoList.Add(pInfo);

                }

                count = 5;
            }

        }

        public void RememberPage(int index, int seconds =0)
        {

            PageInfo pInfo = _pageInfoList.First<PageInfo>(p => p.Id == index);

            if (pInfo != null)
            {
                pInfo.Add(seconds);
            }
            else
            {
                pInfo = new PageInfo();
                pInfo.Id = index;
                pInfo.Add(seconds);
                _pageInfoList.Add(pInfo);
            }
        }

        public int[] GetAdvisePage()
        {
            int[] res = new int[_pageItems];

            int num = 0;
            for (int i = 0; i < _pageInfoArray.Length; i++)
            {
                if (_pageInfoArray[i].IsWorthTest() == true)
                {
                    res[num++] = i;
                }
                if (num >= _pageItems)
                    return res;
            }

            return res;
        }

        public int GetBestPage()
        {
            int max = 0;
            foreach (PageInfo pInfo in _pageInfoList)
            {
                if (pInfo.IsWorthTest() == true)
                {
                    return pInfo.Id;
                }

                if (pInfo.Id > max)
                    max = pInfo.Id;
            }

            return max+1;
        }

        public int GetTryTimes(int index)
        {
            PageInfo pInfo = _pageInfoList.First<PageInfo>(p => p.Id == index);

            if (pInfo != null)
            {
                return pInfo.RemTime.Count;
            }

            return 0;
        }

        public void SaveToFile()
        {
            XDocument xdoc = XmlFile.GetXmlDoc();

            foreach (XElement ifo in xdoc.Root.Elements())
            {
                ifo.Elements("Rem").Remove();
            }

            for (int i = 0; i < _pageInfoArray.Length; i++)
            {
                if (_pageInfoArray[i].RemTime.Count > 0)
                {
                    XElement x = xdoc.Root.Elements().First<XElement>(xe => xe.Element("Id").Value == i.ToString());

                    

                    foreach (DateTime rm in _pageInfoArray[i].RemTime)
                    {
                        x.Add(new XElement("Rem", rm.ToString()));
                    }
                }
            }

            int test = 10;
        
        }
        
    }


    class PageInfo
    {
        //[0] 0     minutes      
        //[1] 5*60     minutes  
        //[2] 30*60    minutes
        //[3] 12*60*60    hours

        //[4] 1*24*60*60     day
        //[5] 2*24*60*60     days
        //[6] 4*24*60*60     days
        //[7] 7*24*60*60     days
        //[8] 15*24*60*60    days

        //static long[] dtDue = new long[9] { 0, 5 * 60 ,30*60,12*60*60,24*60*60,2*24*60*60,4*24*3600,7*24*3600,15*24*3600};
        
        static long[] dtDue = new long[9] { 0, 5 , 30 , 12 * 60, 24 * 60, 2 * 24 * 60, 4 * 24 * 60, 7 * 24 * 60, 15 * 24 * 60 };
        public List<DateTime> RemTime = new List<DateTime>();
        public int[] SpendTime = new int[8];
        public int Id;

        public void Add(int seconds)
        {
            if (RemTime.Count == 0)
            {
                RemTime.Add(DateTime.Now);
                SpendTime[RemTime.Count-1] = seconds;
                return;
            }

            if (RemTime.Count > 0 && RemTime.Count <= 8)
            {
                DateTime d1 = DateTime.Now;

                long dt = (d1.Day - RemTime[RemTime.Count - 1].Day) * 24 * 3600 - (d1.Hour - RemTime[RemTime.Count - 1].Hour) * 3600 - (d1.Minute - RemTime[RemTime.Count - 1].Minute) * 60 + d1.Second - RemTime[RemTime.Count - 1].Second;


                if (dt > dtDue[RemTime.Count] && dt < dtDue[RemTime.Count+1])
                {   
                    RemTime.Add(DateTime.Now);
                    SpendTime[RemTime.Count-1] = seconds;
                }
                if (dt > dtDue[RemTime.Count + 1])
                    RemTime.Clear();
                return;
            }
        }

        public bool IsWorthTest()
        {
            if (RemTime.Count == 0)
                return true;

            DateTime d1 = DateTime.Now;

            long dt = (d1.Day - RemTime[RemTime.Count - 1].Day) * 24 * 3600 - (d1.Hour - RemTime[RemTime.Count - 1].Hour) * 3600 - (d1.Minute - RemTime[RemTime.Count - 1].Minute) * 60 + d1.Second - RemTime[RemTime.Count - 1].Second;

            if (dt > dtDue[RemTime.Count] && dt < dtDue[RemTime.Count + 1])
            {
                return true;
                
            }
            if (dt > dtDue[RemTime.Count + 1])
                RemTime.Clear();
            return false;
        }
    }
}
